function detail(shopId) {
    window.open("http://tl.cyg.changyou.com/goods/char_detail?serial_num=" + shopId);
}
function price(shopId, price, title) {
    layer.open({
        type: 2,
        skin: 'layui-layer-blue',//样式类
        title: title,
        shadeClose: true,
        shade: 0.8,
        area: ['880px', '522px'],
        content: '/Home/price?sId=' + shopId + '&p=' + price
    });
}
function trim(str) {
    return str.replace(/^\s+|\s+$/g, '')
};

function delStr(str, arr) { 
    var n = -1;
    for (var i = 0,
        len = arr.length; i < len; i++) {
        if (str == arr[i]) {
            n = i;
            break;
        }
    }
    n > -1 && arr.splice(n, 1);
};

var sSelect = [];
var dSelect = [];
var dNum = [];
var oClearList = $(".hasBeenSelected .clearList");

function addSearch(_this) {
    var elem = $("select[name='" + $(_this).attr('id') + "']");
    var _text = elem.find("option:selected").val();
    elem.val("");
    form.render();
    var _name = trim($(_this).attr('name'));
    var isCheck = false;
    if (_text != '') {
        var pushStr = _text + '|' + _name;
            if (sSelect.indexOf(pushStr) < 0) {
                sSelect.push(pushStr);
                isCheck = true;
            }
        if (isCheck)
            oClearList.append('<div class=\"selectedInfor selectedShow\"><span>' + _name + '</span><label>' + _text + '</label><em></em></div>')
    }
}
function getArrayIndex(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return i;
        }
    }
    return -1;
}
function doubleSearch(_this) {
    var elem = $("select[name='" + $(_this).attr('id') + "']");
    var numElem = $("#" + $(_this).attr('id') + "_num");
    var _text = elem.find("option:selected").val();
    var _num = numElem.val();
    if (_num == '')
        _num = '0';
    elem.val("");
    numElem.val("");
    form.render();
    var _name = trim($(_this).attr('name'));
    var isCheck = false;
    var isUpdate = false;
    if (_text != '') {
        var pushStr = _text + '|' + _name + '|' + _num;
        
        dSelect.forEach(function (item, index, arr) {
            if (item.indexOf(_text + '|' + _name) >= 0 && item.replace(_text + '|' + _name + '|', "") != _num) {
                arr.splice(index, 1, pushStr);
                console.log(item);
                isUpdate = true;
            }
        });
        if (!isUpdate && dSelect.indexOf(pushStr) < 0) {
            dSelect.push(pushStr);
            isCheck = true;
        }

        if (isCheck && !isUpdate)
            oClearList.append('<div class=\"selectedInfor selectedShow\"><span>' + _name + '</span><label>' + _text + '</label><a>' + _num + '</a><em></em></div>');
        else
        {
            var infor = '';
            for (var i = 0; i < sSelect.length; i++) {
                var valChecks = sSelect[i].split('|');
                infor += '<div class=\"selectedInfor selectedShow\"><span>' + valChecks[1] + '</span><label>' + valChecks[0] + '</label><em></em></div>';
            }
            for (var i = 0; i < dSelect.length; i++) {
                var valChecks = dSelect[i].split('|');
                infor += '<div class=\"selectedInfor selectedShow\"><span>' + valChecks[1] + '</span><label>' + valChecks[0] + '</label><a>' + valChecks[2] + '</a><em></em></div>';
            }

            oClearList.html(infor); 
        }

    }
}



$('div.eliminateCriteria').click(function () {
    sSelect = [];
    dSelect = [];
    oClearList.html("");
    $("#tab_div input").val("");
    $("#tab_div input[type='checkbox']").each(function () { this.checked = false; });
});

$('.clearList').find('em').live('click', function () {
    var self = $(this);
    var val = self.prev().html() + '|' + self.prev().prev().html();
    delStr(val, sSelect);

    var val2 = self.prev().prev().html() + '|' + self.prev().prev().prev().html() + '|' + self.prev().html();
    delStr(val2, dSelect);


    var infor = '';
    for (var i = 0; i < sSelect.length; i++) {
        var valChecks = sSelect[i].split('|');
        infor += '<div class=\"selectedInfor selectedShow\"><span>' + valChecks[1] + '</span><label>' + valChecks[0] + '</label><em></em></div>';
    }
    for (var i = 0; i < dSelect.length; i++) {
        var valChecks = dSelect[i].split('|');
        infor += '<div class=\"selectedInfor selectedShow\"><span>' + valChecks[1] + '</span><label>' + valChecks[0] + '</label><a>' + valChecks[2] + '</a><em></em></div>';
    }

    oClearList.html(infor); 
})



function shending(count) {
    var sHtml = count;
    var level = 0;
    for (var i = 9; i > 1; i--) {
        if ((1 + i) * (i / 2) <= count) {
            level = i;
            break;
        }
    }
    if (level == 9)
        sHtml = '<span style="color:red;">9</span>';
    else if (level == 8)
        sHtml = '<span style="color:orange;">8</span>';
    else if (level == 7)
        sHtml = '<span style="color:greenyellow;">7</span>';
    else if (level == 6)
        sHtml = '<span style="color:blue;">6</span>';
    else if (level == 5)
        sHtml = '<span style="color:aqua;">5</span>';
    else
        sHtml = level;
    return sHtml;
}
function menpai(mp) {
    var sHtml = '';
    switch (mp) {
        case 0: sHtml = '少林';
            break;
        case 1: sHtml = '明教';
            break;
        case 2: sHtml = '丐帮';
            break;
        case 3: sHtml = '武当';
            break;
        case 4: sHtml = '峨嵋';
            break;
        case 5: sHtml = '星宿';
            break;
        case 6: sHtml = '天龙';
            break;
        case 7: sHtml = '天山';
            break;
        case 8: sHtml = '逍遥';
            break;
        case 10: sHtml = '慕容';
            break;
        case 11: sHtml = '唐门';
            break;
        case 12: sHtml = '鬼谷';
            break;
        case 13: sHtml = '桃花岛';
            break;
        case 14: sHtml = '绝情谷';
            break;
    }
    return sHtml;
}
function sex(s) {
    var sHtml = '女';
    switch (s) {
        case 1: sHtml = '男';
            break;
    }
    return sHtml;
}
function limitMax(resistColdDefLimit, resistFireDefLimit, resistLightDefLimit, resistPostionDefLimit) {
    return Math.max(resistColdDefLimit, resistFireDefLimit, resistLightDefLimit, resistPostionDefLimit);
}
function att(resistColdDef, resistFireDef, resistLightDef, resistPostionDef) {
    var sHtml = '';
    var _max = Math.max(resistColdDef, resistFireDef, resistLightDef, resistPostionDef);

    if (_max == resistColdDef)
        sHtml = '<span style="font-weight: bold;">' + resistColdDef + '</span> | ';
    else
        sHtml = resistColdDef + ' |';

    if (_max == resistFireDef)
        sHtml += '<span style="font-weight: bold;">' + resistFireDef + '</span> | ';
    else
        sHtml += resistFireDef + ' |';

    if (_max == resistLightDef)
        sHtml += '<span style="font-weight: bold;">' + resistLightDef + '</span> | ';
    else
        sHtml += resistLightDef + ' |';

    if (_max == resistPostionDef)
        sHtml += '<span style="font-weight: bold;">' + resistPostionDef + '</span>';
    else
        sHtml += resistPostionDef;

    return sHtml;
}
function shengh(s1, s2, s3, s4, s5, s6) {
   
    return s1 + " | " + s2 + " | " + s3 + " | " + s4 + " | " + s5 + " | " + s6;
}
function cl(count) {
    var sHtml = '';
    if (count > 0)
        sHtml = '<i class="icon-cl" title="该角色拥有重楼装备">&nbsp;</i>';
    return sHtml;
}
function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
function szIcon(ct, data) {
    var _id = guid();
    var sHtml = "<div style='float:left;width:70px;'>时装：" + ct + "</div><div class='ofw' style='margin-left:70px;height:23px;' id='" + _id + "'>";
    for (var i = 0; i < data.length; i++) {
        sHtml += '<div class="imgDiv" title="' + data[i].title + ' &nbsp; ' + data[i].style + '">';
        sHtml += '<b class="s-default">';
        sHtml += '<i class="s-count s-count' + data[i].cName + '" style="background-image:url(http://image.cyg.changyou.com/tl/small/' + data[i].imgUrl + '.jpg?v=20140806);"></i>';
        sHtml += '</b>';
        sHtml += '</div>';
    };
    sHtml += '<i class="layui-icon layui-icon-down" onclick="ShowAll(this,\'' + _id + '\')"></i>  ';
    sHtml += '</div>';
    return sHtml;
}
function ShowAll(_this, _id) {
    var sHtml = $(_this).html();
    if ($(_this).hasClass("layui-icon-down")) {
        $(_this).removeClass("layui-icon-down")
        $(_this).addClass("layui-icon-up")
    } else {
        $(_this).removeClass("layui-icon-up")
        $(_this).addClass("layui-icon-down")
    }
    $("#" + _id).toggleClass("ofw");
}
function hsIcon(ct, data) {
    var sHtml = "<div style='float:left;width:70px;'>幻饰：" + ct + "</div><div>";
    for (var i = 0; i < data.length; i++) {
        sHtml += '<div class="imgDiv" title="' + data[i].title + '">';
        sHtml += '<b class="s-default">';
        sHtml += '<i class="s-count s-count' + data[i].cName + '" style="background-image:url(http://image.cyg.changyou.com/tl/small/' + data[i].imgUrl + '.jpg?v=20140806);"></i>';
        sHtml += '</b>';
        sHtml += '</div>';
    };
    sHtml += '</div>';
    return sHtml;
}
function zqIcon(data) {
    var sHtml = "<div style='float:left;width:70px;'>坐骑：</div><div>";
    for (var i = 0; i < data.length; i++) {
        sHtml += '<div class="imgDiv" title="' + data[i].title + '">';
        sHtml += '<b class="s-default">';
        sHtml += '<i class="s-count s-count' + data[i].cName + '" style="background-image:url(http://image.cyg.changyou.com/tl/small/' + data[i].imgUrl + '.jpg?v=20140806);"></i>';
        sHtml += '</b>';
        sHtml += '</div>';
    };
    sHtml += '</div>';
    return sHtml;
}
function xjIcon(data) {
    var sHtml = "<div style='float:left;width:70px;'>心决：</div><div>";
    for (var i = 0; i < data.length; i++) {
        sHtml += '<div class="imgDiv" title="' + data[i].title + ' &nbsp; ' + data[i].style + '级">';
        sHtml += '<b class="s-default">';
        sHtml += '<i class="s-count s-count' + data[i].cName + '" style="background-image:url(http://image.cyg.changyou.com/tl/tlsc-80/' + data[i].imgUrl + '.jpg?v=20140806);"></i>';
        sHtml += '</b>';
        sHtml += '</div>';
    };
    sHtml += '</div>';
    return sHtml;
}
function mjIcon(data) {
    var sHtml = "<div style='float:left;width:70px;'>秘技：</div><div>";
    for (var i = 0; i < data.length; i++) {
        sHtml += '<div class="imgDiv" title="' + data[i].title + ' &nbsp; ' + data[i].style + '级">';
        sHtml += '<b class="s-default">';
        sHtml += '<i class="s-count s-count' + data[i].cName + '" style="background-image:url(http://image.cyg.changyou.com/tl/tlsc-80/' + data[i].imgUrl + '.jpg?v=20140806);"></i>';
        sHtml += '</b>';
        sHtml += '</div>';
    };
    sHtml += '</div>';
    return sHtml;
}
function ClearValue(_id) {
    $("[name='" + _id + "']").removeAttr("checked");
    $("#" + _id + "1").val("");
    $("#" + _id + "2").val("");
}

function addDay(day, date) {
    date = date ? date : new Date();
    var ms = day * (1000 * 60 * 60 * 24)
    var newDate = new Date(date.getTime() + ms);
    return newDate;
}

function soCount(diffTime, _str) {
    console.log(diffTime);
    var d, h, m, s;
    d = Math.floor(diffTime / 60 / 60 / 24);
    h = Math.floor(diffTime / 60 / 60) % 24;
    m = Math.floor(diffTime / 60) % 60;
    s = Math.floor(diffTime) % 60;

    diffTime -= 1;
    clearT = setTimeout(soCount(diffTime), 1000);
    if (diffTime < 0) {
        clearTimeout(clearT);
        return "当前状态：<span color:'red'>已下架<span>";
    }
    return _str + '：<span class="day">' + d + '天' + (h.toString().length == 1 ? '0' + h : h) + '小时' + (m.toString().length == 1 ? '0' + m : m) + '分</span>';
}

function remainedTimeAct(cls) {
    var dqTime = new Date();
    $(cls).each(function () {
        (function (that) {
            var d = new Date(that.html());
            var gsTime = addDay(7, d);
            var jyTime = addDay(21, d);
            var gsSecond = (gsTime.getTime() - dqTime.getTime()) / 1000;
            var _date = gsSecond > 0 ? gsSecond : (jyTime.getTime() - dqTime.getTime()) / 1000;
            var _str = gsSecond > 0 ? "公示期剩余时间" : "交易区剩余时间";

            var d, h, m, s, clearT;
            function soCount() {
                d = Math.floor(_date / 60 / 60 / 24);
                h = Math.floor(_date / 60 / 60) % 24;
                m = Math.floor(_date / 60) % 60;
                s = Math.floor(_date) % 60;
                if (_date <= 0)
                    that.html("当前状态：<span style='color: red'>已下架</span>");
                else if (_date < 86400) {
                    _date -= 1;
                    clearT = setTimeout(soCount, 1000);
                    if (_date < 0) {
                        clearTimeout(clearT);
                        that.html("当前状态：<span style='color: red'>已下架</span>");
                    }
                    that.html(_str + '：<span class="day" style="color: #ec7a00">' + (h.toString().length == 1 ? '0' + h : h) + '小时' + (m.toString().length == 1 ? '0' + m : m) + '分' + (s.toString().length == 1 ? '0' + s : s) + '秒</span>');
                }
                else
                    that.html(_str + '：<span class="day">' + d + '天' + (h.toString().length == 1 ? '0' + h : h) + '小时' + (m.toString().length == 1 ? '0' + m : m) + '分</span>');
            }
            soCount();
        })($(this));
    });
}